<?php
final class MySQL {
   private $connection;
   private $connection_stored_procedure; // this is one chamge
   private $i_last_query; // this is one chamge
   private $hostname, $username, $password, $database;
   
   public function __construct($hostname, $username, $password, $database) {
   	$this->hostname = $hostname;
   	$this->password = $password;
   	$this->username = $username;
   	$this->database = $database;
   	
      $i_last_query=1;
      if (!$this->connection = mysql_connect($hostname, $username, $password)) {
            exit('Error: Could not make a database connection using ' . $username . '@' . $hostname);
       }
      
      
      
       if (!mysql_select_db($database, $this->connection)) {
            exit('Error: Could not connect to database ' . $database);
       }
      
      mysql_query("SET NAMES 'utf8'", $this->connection);
      mysql_query("SET CHARACTER SET utf8", $this->connection);
      mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $this->connection);
      mysql_query("SET SQL_MODE = ''", $this->connection);
     }
      
     public function query($sql) {
      $this->i_last_query=1;
      $resource = mysql_query($sql, $this->connection);

      if ($resource) {
         if (is_resource($resource)) {
            $i = 0;
       
            $data = array();
      
            while ($result = mysql_fetch_assoc($resource)) {
               $data[$i] = $result;
       
               $i++;
            }
            
            mysql_free_result($resource);
            
            $query = new stdClass();
            $query->row = isset($data[0]) ? $data[0] : array();
            $query->rows = $data;
            $query->num_rows = $i;
            
            unset($data);

            return $query;   
          } else {
            return TRUE;
         }
      } else {
            exit('Error: ' . mysql_error($this->connection) . '<br />Error No: ' . mysql_errno($this->connection) . '<br />' . $sql);
       }
     }
   
   // this is one chamge
   public function sp_query($sql) {
      $this->i_last_query=2;

      // this is one chamge
      if (!$this->connection_stored_procedure= new mysqli($this->hostname, $this->username, $this->password, $this->database)) {
            exit('Error: Could not make a database [stored procedure] connection using ' . $this->username . '@' . $this->hostname);
       }
      $sqlSuccess = $this->connection_stored_procedure->multi_query($sql);      
      
      if ($sqlSuccess) {
         
         if ($this->connection_stored_procedure->more_results()) {
            
            $i = 0;
            
            $data = array();
      
            // Get the first buffered result set, the one with our data.
                $resource = $this->connection_stored_procedure->use_result();
            
            
            while ($result = $resource->fetch_assoc()) {
               $data[$i] = $result;       
               $i++;
            }
                        
            // Free the first resource set.
                // If you forget this one, you will get the "out of sync" error.
                $resource->free();
            
            
                // Go through each remaining buffered result and free them as well.
                // This removes all extra result sets returned, clearing the way
                // for the next SQL command.
                while($this->connection_stored_procedure->more_results() && $this->connection_stored_procedure->next_result())
                {
                    $extraResult = $this->connection_stored_procedure->use_result();
                    if($extraResult instanceof mysqli_result){
                        $extraResult->free();
                    }
                }
            
            
            $query = new stdClass();
            $query->row = isset($data[0]) ? $data[0] : array();
            $query->rows = $data;
            $query->num_rows = $i;
            
            unset($data);

            return $query;   
          } else {
            
            return TRUE;
         }
      } else {
            exit('Error: <br />Error No: ' . $this->connection_stored_procedure->error . '<br />' . $sql);
       }
     }
      
   // this is one chamge
   public function escape($value) {

   	try{
      if (isset($this->i_last_query))
      {
      if ($this->i_last_query==1)
         {return mysql_real_escape_string($value, $this->connection);}
      else
         {return mysqli_real_escape_string($value, $this->connection_stored_procedure);}
      }
      else
         {
         return mysql_real_escape_string($value, $this->connection);
         }
   	}catch (Exception $ex){}
         //return mysql_real_escape_string($value, $this->connection);
   }
   
   // this is one chamge
     public function countAffected() {

     	try{
      if (isset($this->i_last_query))
      {
         if ($this->i_last_query==1)
            {return mysql_affected_rows($this->connection);}
         else
            {return mysqli_affected_rows($this->connection_stored_procedure);}
      }
      else
      {return mysql_affected_rows($this->connection);}
     	}catch (Exception $ex){}
     }

   // this is one chamge
     public function getLastId() {

     	try{
      if (isset($this->i_last_query))
      {
         if ($this->i_last_query==1)
            {return mysql_insert_id($this->connection);}
         else
            {return mysqli_insert_id($this->connection_stored_procedure);}
      }
      else {return mysql_insert_id($this->connection);}
     	}catch (Exception $ex){}
     }   
   
   // this is one chamge
   public function __destruct() {
   	
   	try{
   if (isset($this->i_last_query))
      {
         if ($this->i_last_query==1)
            {mysql_close($this->connection);}
         else
            {mysql_close($this->connection_stored_procedure);}
      }
      else {mysqli_close($this->connection);}
      
   	}catch (Exception $ex){}
   }

   
}
?>